<?php
class Accountinfo extends CS_Controller
{
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->helper('common');
        $this->load->model('user_model', 'user');
        $this->load->model('user_account_model', 'user_account');
        $this->load->model('account_log_model', 'account_log');
        $this->load->model('carry_log_model', 'carry_log');
        $this->load->model('recharge_log_model', 'recharge_log');
        $this->load->model('rebate_log_model', 'rebate_log');
        $this->load->model('pay_method_model', 'pay_method');
        $this->load->model('user_account_list_model', 'user_account_list');
        $this->load->model('withdraw_model', 'withdraw');
        $this->load->model('deposit_model', 'deposit');
        $this->load->model('tourism_provider_model', 'tourism_provider');
        $this->load->model('tourism_service_category_model', 'tourism_service_category');
        $this->load->model('user_related_self_model', 'user_related_self');
    }
    
    /**
     * 个人信息
     */
    public function personal()
    {
        $result = $this->user->findOne(['uid'=>$this->uid]);
        $data['useInfo'] = $result->row();
        $userAccount = $this->user_account->findByUid($this->uid);
        $data['balance'] = $userAccount->row();
        $this->load->view('accountinfo/personal', $data);
    }
    
    /**
     * 验证用户是否注册过。
     */
    public function validateMobilePhone()
    {
        $result = $this->user->validateMobilePhone($this->input->post());
        if ( $result->num_rows() > 0 ) {
            echo 'false';
        } else {
            echo 'true';
        }
        exit;
    }
    
    /**
     * 提交修改个人信息
     */
    public function editPostPersonal()
    {
        $postData    = $this->input->post();
        $mobilePhone = $this->input->post('mobile_phone');
        $cellphone   = $this->input->post('cellphone');
        $sex_id      = $this->input->post('sex_id');
        $birthday    = $this->input->post('birthday');
        if ($mobilePhone && !valid_mobile($mobilePhone)) {
            $this->error('accountinfo/personal', '', '手机号码有误，请检查~');
        }
        if ($cellphone && !valid_mobile($cellphone)) {
            $this->error('accountinfo/personal', '', '备用手机有误，请检查~');
        }
        if ($sex_id && !in_array($sex_id, [0, 1, 2])) {
            $this->error('accountinfo/personal', '', '性别不合法~');
        }
        if ($birthday) {
            $isDate = strtotime($birthday) ? strtotime($birthday) : false;
            if (!$isDate) {
                $this->error('accountinfo/personal', '', '生日不合法~');
            }
        }
        $update = $this->user->update($postData, ['uid' => $this->uid]);
        if ($update && $this->db->trans_status() === TRUE) {
            $this->success('accountinfo/personal', '', '保存成功！');
        } else {
            $this->error('accountinfo/personal', '', '保存失败！');
        }
    }

    /**
     * 店铺信息
     */
    public function storeInfo()
    {
        $store = $this->tourism_provider->findOne(['uid'=>$this->uid]);
        $data['storeInfo'] = $store->row();
        $category = $this->tourism_service_category->findAll(['status' => 1], 'id, name');
        $data['category'] = $category->result();
        $this->load->view('accountinfo/storeInfo', $data);
    }

    /**
     * 提交修改店铺信息
     */
    public function editStoreInfo()
    {
        $postData = $this->input->post();
        if (!empty($this->input->post('service_qq'))){
            $service_qq = trim(strtr($this->input->post('service_qq'), array('，'=>',')), ',');
            $postData['service_qq'] = $service_qq;
        }
        $category_id = (int)$this->input->post('category_id');
        $mobilePhone = $this->input->post('mobile_phone');
        if ($category_id && !is_int($category_id)) {
            $this->error('accountinfo/storeInfo', '', '商品分类有误~');
        }
        if ($mobilePhone && !valid_mobile($mobilePhone)) {
            $this->error('accountinfo/storeInfo', '', '备用手机有误，请检查~');
        }
        $update = $this->tourism_provider->update($postData, ['uid' => $this->uid]);
        if ($update && $this->db->trans_status() === TRUE) {
            $this->success('accountinfo/storeInfo', '', '保存成功！');
        } else {
            $this->error('accountinfo/storeInfo', '', '保存失败！');
        }
    }

    /**
     * 账户收支
     */
    public function accountRecord($pg = 1)
    {
        $page_num = 15;
        $num = ($pg-1)*$page_num;
        $config['first_url']   = base_url('accountinfo/accountRecord').$this->pageGetParam($this->input->get());
        $config['suffix']      = $this->pageGetParam($this->input->get());
        $config['base_url']    = base_url('accountinfo/accountRecord');
        $config['total_rows']  = $this->account_log->total(array('uid'=>$this->uid));
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['accountRecord'] = $this->account_log->page_list($page_num, $num, array('uid'=>$this->uid));
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $data['userAccount'] = $this->user_account->findByUid($this->uid)->row();
        $data['levelType'] = $this->levelType();
        $data['accountType'] = $this->accountType();
        $this->load->view('accountinfo/accountRecord', $data);
    }
    
    /**
     * 提现记录
     */
    public function carryRecord($pg=1)
    {
        $page_num = 15;
        $num = ($pg-1)*$page_num;
        $config['first_url']   = base_url('accountinfo/carryRecord').$this->pageGetParam($this->input->get());
        $config['suffix']      = $this->pageGetParam($this->input->get());
        $config['base_url']    = base_url('accountinfo/carryRecord');
        $config['total_rows']  = $this->withdraw->total(array('uid'=>$this->uid));
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['withdrawList'] = $this->withdraw->page_list(array('uid'=>$this->uid), $page_num, $num);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $data['userAccount'] = $this->user_account->findByUid($this->uid)->row();
        $this->load->view('accountinfo/carryRecord', $data);
    }
    
    /**
     * 提现记录
     */
    public function rechargeRecord($pg=1)
    {
        $page_num = 15;
        $num = ($pg-1)*$page_num;
        $config['first_url']   = base_url('accountinfo/rechargeRecord').$this->pageGetParam($this->input->get());
        $config['suffix']      = $this->pageGetParam($this->input->get());
        $config['base_url']    = base_url('accountinfo/rechargeRecord');
        $config['total_rows']  = $this->deposit->total(array('uid'=>$this->uid));
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['depositList'] = $this->deposit->page_list(array('uid'=>$this->uid), $page_num, $num);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $data['userAccount'] = $this->user_account->findByUid($this->uid)->row();
        $this->load->view('accountinfo/rechargeRecord', $data);
    }
    
    /**
     * 返利记录
     */
    public function rebateRecord($pg=1)
    {
        $page_num = 15;
        $num = ($pg-1)*$page_num;
        $config['first_url']   = base_url('accountinfo/rebateRecord').$this->pageGetParam($this->input->get());
        $config['suffix']      = $this->pageGetParam($this->input->get());
        $config['base_url']    = base_url('accountinfo/rebateRecord');
        $config['total_rows']  = $this->rebate_log->total(array('uid'=>$this->uid));
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['rebateRecord'] = $this->rebate_log->page_list($page_num, $num, array('uid'=>$this->uid));
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $data['userAccount'] = $this->user_account->findByUid($this->uid)->row();
        $this->load->view('accountinfo/rebateRecord', $data);
    }
    
    /**
     * 下级会员
     */
    public function JuniorMember($pg=1)
    {
        $page_num = 15;
        $num = ($pg-1)*$page_num;
        $config['first_url']   = base_url('accountinfo/JuniorMember').$this->pageGetParam($this->input->get());
        $config['suffix']      = $this->pageGetParam($this->input->get());
        $config['base_url']    = base_url('accountinfo/JuniorMember');
        $config['total_rows']  = $this->user->total(array('uid'=>$this->uid));
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->user->page_list($page_num, $num, array('uid'=>$this->uid));
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $this->load->view('accountinfo/JuniorMember',$data);
    }
    
    /**
     * 银行提现
     */
    public function bankCash()
    {
        $this->load->view('accountinfo/bankCash');
    }

    /**
     * 安全设置
     */
    public function securitySettings()
    {
        $data['userInfo'] = $this->user->findOne(['uid' => $this->uid])->row();
        $res = $this->user_related_self->findOne(['provider_uid' => $this->uid]);
        if ($res->num_rows() <= 0) {
            $data['seller'] = false;
        }else {
            $sellerRes = $this->user->findOne(['uid' => $res->row()->reseller_uid]);
            $seller = $sellerRes->row();
            $sell = $seller->user_name?:$seller->mobile_phone;
            $data['seller'] = $sell;
        }
        $this->load->view('accountinfo/securitySettings',$data);
    }
    
    //结算
    function rebate()
    {
        $result = $this->user_account->findByUid($this->uid);
        $data['userAccount'] = $result->row();
        $this->load->view('accountinfo/rebate', $data);
    }
    
    /**
     * 执行结算操作
     */
    function rebateDo()
    {
        if ($this->input->post('o_income') <= 0) {
            $this->error('accountinfo/rebate', '', '结算金额必须要大于零');
        }
        $result = $this->user->getUserAccount(array('uid'=>$this->uid));
        if ($result->num_rows() <= 0) {
            $this->error('accountinfo/rebate', '', '获取用户账户信息失败');
        }
        $userAccount  = $result->row();
        
        if (!((int)$userAccount->user_type&UTID_PROVIDER)) {//UTID_PROVIDER 供应商
            $this->error('accountinfo/rebate', '', '不是供应商账号，不能操作。');
        }
        if ($userAccount->amount_month > 0) {
            $this->error('accountinfo/rebate', '', '月结账号数据有误');
        }
        if ($this->input->post('o_income') > abs($userAccount->amount_month)) {
            $this->error('accountinfo/rebate', '', '申请金额超限');
        }
        if ($this->input->post('deduction') > $userAccount->amount_carry) {
            $this->error('accountinfo/rebate', '', '打款金额小于抵扣金额');
        }
        if ($this->input->post('o_income') < $this->input->post('deduction')) {
            $this->error('accountinfo/rebate', '', '抵扣金额大于打款金额');
        }
        
        $postData = $this->input->post();
        $postData['uid']            = $this->uid;
        $postData['r_income']       = $this->input->post('o_income') - $this->input->post('deduction');
        $postData['n_amount_carry'] = $userAccount->amount_carry - $this->input->post('deduction');
        $postData['n_amount_month'] = $userAccount->amount_month + $this->input->post('o_income');
        
        $postData['order_id']       = 0;
        $postData['type']           = DEDUCTION;
        $postData['account_type']   = ACCOUNT_TYPE_CARRY;
        $postData['product_type']   = 'tourism';
        
        $this->db->trans_start();
        if ($this->input->post('o_income') - $this->input->post('deduction') > 0) { //如果可提现金额大于零
            $result = $this->user_account_list->insertUserAccountList($postData);
        }
        $update = $this->user_account->updateUserAccount($this->uid, $postData);
        
        $insertId = $this->account_log->insertAccountLog($postData);
        $this->db->trans_complete();
        
        if ($this->db->trans_status() === TRUE && $update && $insertId) {
            $this->success('accountinfo/rebateRecord', '', '申请成功，请等待审核！');
        } else {
            $this->error('accountinfo/rebate', '', '抵扣金额大于打款金额');
        }
    }
    
    /**
     * 账户充值
     */
    public function incoming()
    {
        $data['payment'] = $this->pay_method->getAllBankId();
        $this->load->view('accountinfo/incoming', $data);
    }
    
    /**
     * 账户提现
     */
    public function outcoming()
    {
        $result = $this->user->findOne(['uid'=>$this->uid]);
        $data['userInfo'] = $result->row();
        $userAccount = $this->user_account->findOne(['uid' => $this->uid]);
        $data['balance'] = $userAccount->row();
        $this->load->view('accountinfo/outcoming', $data);
    }
}